using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
using ReflectionStudio.Core.Events;
using System;
namespace WPFExcelReport
{
    public partial class VVVPACK
    {
        public DataRow dwVVP;
        public static readonly VVVPACK Instance = new VVVPACK();
        public void Increase(DataRow _dwVVP, MessageInfo msg)
        {

            double current = 0;
            double increased = 0;
            double brt = 0;

            current = Convert.ToDouble(_dwVVP["VVPBRUTTO"].ToString());
            increased = Convert.ToDouble(msg.P2) / 10;
            _dwVVP["VVPBRUTTO"] = current + increased;
            _dwVVP["VVPNETTO"] = _dwVVP["VVPBRUTTO"];
            current = Convert.ToDouble(_dwVVP["VVPSPULEN"].ToString());
            increased = 1;
            _dwVVP["VVPSPULEN"] = current + increased;

        }
        public void Update(MessageInfo msg)
        {
            Increase(dwVVP, msg);
            UpdateVVPACK(dwVVP, app.company, 0);
        }
         
        public DataSet NVVPACK(MessageInfo msg)
        { 
            DataSet SResult = null;
            string filter = "VVPLIEFSCH='" + msg.LIEFSCH + "' AND VVPPACKNR='0001'";
            DataRow[] rows = TBMain.Select(filter);
            dwVVP = null;
            foreach (DataRow row in rows)
            {
                dwVVP = row;
            }
            if (dwVVP != null)
            {
                Update(msg);
                return SetProd;
            }
            DataRow dwData = TBMain.NewRow();
            dwData["VVPMANDANT"] = app.Mandant;
            dwData["VVPLIEFSCH"] = msg.LIEFSCH;
            dwData["VVPPACKNR"] = "0001";
            SResult = QueryVVPACK(dwData, 1);
            SetProd.Clear();
            SetProd.Merge(SResult);
            if (TBMain.Rows.Count == 0)
                New(msg);
            return SetProd;
        }

        public void New(MessageInfo msg)
        {
            DataRow dwData = SetProd.Tables[TableName].NewRow();
            double value = 0;
            SetUpNewRow(ref dwData);
            #region=========
            dwData["VVPMANDANT"] = app.Mandant;
            dwData["VVPLIEFSCH"] = msg.LIEFSCH;
            dwData["VVPPACKNR"] = "0001";
            dwData["VVPUEBLIEFSCH"] = dwData["VVPLIEFSCH"];
            dwData["VVPPACKTYP"] = "01  ";
            Increase(dwData, msg);
            #endregion
            InsertVVPACK(dwData, app.company);
            SetProd.Tables[TableName].Rows.InsertAt(dwData, 0);
        }
    } 
}
